/**
	 * 中国公民身份证号码最大长度。
	 */
const CHINA_ID_MAX_LENGTH = 18
/**
	 * 每位加权因子
	 */
const power = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
const city = {
  11: '北京',
  12: '天津',
  13: '河北',
  14: '山西',
  15: '内蒙古',
  21: '辽宁',
  22: '吉林',
  23: '黑龙江 ',
  31: '上海',
  32: '江苏',
  33: '浙江',
  34: '安徽',
  35: '福建',
  36: '江西',
  37: '山东',
  41: '河南',
  42: '湖北 ',
  43: '湖南',
  44: '广东',
  45: '广西',
  46: '海南',
  50: '重庆',
  51: '四川',
  52: '贵州',
  53: '云南',
  54: '西藏 ',
  61: '陕西',
  62: '甘肃',
  63: '青海',
  64: '宁夏',
  65: '新疆',
  83: '台湾',
  81: '香港',
  82: '澳门',
  91: '国外 '
}
// 外国人永久居留身份证老版每位加权因子
const WIGHT = [7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3]
// 外国人永久居留身份证老版 字母转对应数字
const CHAR_CODE = {
  A: 10,
  B: 11,
  C: 12,
  D: 13,
  E: 14,
  F: 15,
  G: 16,
  H: 17,
  I: 18,
  J: 19,
  K: 20,
  L: 21,
  M: 22,
  N: 23,
  O: 24,
  P: 25,
  Q: 26,
  R: 27,
  S: 28,
  T: 29,
  U: 30,
  V: 31,
  W: 32,
  X: 33,
  Y: 34,
  Z: 35
}
export const isValidForeignCard18 = idCard => {
  const result = {}
  result.status = true
  result.errtips = ''
  if (CHINA_ID_MAX_LENGTH != idCard.length) {
    result.status = false
    result.errtips = '外国人永久居留身份证格式错误'
    return result
  }

  // 9开头
  if (idCard.substring(0, 1) != '9') {
    result.status = false
    result.errtips = '外国人永久居留身份证格式错误'
    return result
  }

  // 省份
  const proCode = idCard.substring(1, 3)
  if (!city[proCode]) {
    result.status = false
    result.errtips = '外国人永久居留身份证格式错误'
    return result
  }

  // 校验生日
  // if (!/^((?:19[2-9]\d{1})|(?:20(?:(?:0[0-9])|(?:1[0-9])|(?:2[0-4]))))((?:0?[1-9])|(?:1[0-2]))((?:0?[1-9])|(?:[1-2][0-9])|30|31)$/i.test(idCard.substring(6, 14))) {
  //   result.status = false
  //   result.errtips = '外国人永久居留身份证格式错误'
  //   return result
  // }

  // 前17位
  var code17 = idCard.substring(0, 17)
  // 第18位
  var code18 = idCard.charAt(17).toLowerCase()
  if (/^[0-9]*$/i.test(code17)) {
    // 获取校验位
    var val = getCheckCode18(getPowerSum(code17.split('')))
    if (val == code18) {
      result.status = true
      result.errtips = '外国人永久居留身份证格式正确'
      // 获取出生年月日
      var yearBirth = idCard.substring(6, 10) // 出生年份
      var monthBirth = idCard.substring(10, 12) // 出生月份
      var dayBirth = idCard.substring(12, 14) // 出生日
      const birth = yearBirth + monthBirth + dayBirth
      const birthday = birth.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3')
      const age = getAge(birthday)
      result.age = age
      return result
    }
  }
  result.status = false
  result.errtips = '外国人永久居留身份证格式错误'
  return result
}

export const getPowerSum = iArr => {
  var iSum = 0
  if (power.length == iArr.length) {
    for (var i = 0; i < iArr.length; i++) {
      iSum += parseInt(iArr[i]) * power[i]
    }
  }
  return iSum
}
export const getCheckCode18 = iSum => {
  switch (iSum % 11) {
    case 10:
      return '2'
    case 9:
      return '3'
    case 8:
      return '4'
    case 7:
      return '5'
    case 6:
      return '6'
    case 5:
      return '7'
    case 4:
      return '8'
    case 3:
      return '9'
    case 2:
      return 'x'
    case 1:
      return '0'
    case 0:
      return '1'
    default:
      return ''
  }
}

export const isValidForeignCard15 = idCard => {
  const resultData = {
    status: false,
    tip: '外国人永久居留身份证格式错误',
    age: null
  }
  const reg = /^[a-zA-Z]{3}\d{12}$/
  if (!reg.test(idCard)) {
    return resultData
  }
  if (!validParityBit(idCard)) {
    return resultData
  }
  const birthday = getForeignBirth15(idCard)
  const age = getAge(birthday)
  resultData.age = age
  resultData.tip = '外国人永久居留身份证格式正确'
  resultData.status = true
  return resultData
}

export const getAge = strBirthday => {
  var returnAge
  var strBirthdayArr = strBirthday.split('-')
  var birthYear = strBirthdayArr[0]
  var birthMonth = strBirthdayArr[1]
  var birthDay = strBirthdayArr[2]

  var d = new Date()
  var nowYear = d.getFullYear()
  var nowMonth = d.getMonth() + 1
  var nowDay = d.getDate()

  if (nowYear == birthYear) {
    returnAge = 0 // 同年 则为0岁
  } else {
    var ageDiff = nowYear - birthYear // 年之差
    if (ageDiff > 0) {
      if (nowMonth == birthMonth) {
        var dayDiff = nowDay - birthDay // 日之差
        if (dayDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      } else {
        var monthDiff = nowMonth - birthMonth // 月之差
        if (monthDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      }
    } else {
      returnAge = -1 // 出生日期不能大于今天
    }
  }
  return returnAge
}

export const getForeignBirth15 = idCard => {
  const year = idCard.substring(7, 8)
  let birth = ''
  if (Number(year) >= 0 && Number(year) <= 3) {
    birth = '20' + idCard.substring(7, 13)
  } else {
    birth = '19' + idCard.substring(7, 13)
  }
  return birth.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3')
}

export const getPowerSum15 = idCard => {
  let iSum = 0
  const idList = []
  for (let a = 0; a < idCard.length; a++) {
    // 前三位字母
    if (a < 3) {
      const str = CHAR_CODE[idCard.substring(a, a + 1)]
      idList.push(str)
    } else {
      idList.push(Number(idCard.substring(a, a + 1)))
    }
  }
  for (let i = 0; i < WIGHT.length; i++) {
    iSum += idList[i] * WIGHT[i]
  }
  return iSum
}

export const validParityBit = idCard => {
  // 前14位
  const code14 = idCard.substring(0, 14)
  // 第15位
  const code15 = Number(idCard.substring(14, 15))

  // 获取校验位
  const powerSum15 = getPowerSum15(code14)
  const ys = powerSum15 % 10
  return ys == code15
}
